//
//  MPSHandleSessionManager.m
//  MiaoPai
//
//  Created by jiaxuzhou on 2017/5/24.
//  Copyright © 2017年 jiaxuzhou. All rights reserved.
//

#import "MPSHandleSessionManager.h"
#import "MPBNetWorking.h"
#import "MPCURLSessionTask+MPCNetWork.h"

/// 单例变量
static MPSHandleSessionManager *instance=nil;


@interface MPSHandleSessionManager()
<
UIAlertViewDelegate
>

/// alertView容器
@property (nonatomic, strong) NSMutableDictionary *mdicALert;


@end


@implementation MPSHandleSessionManager

+ (MPSHandleSessionManager *)getInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
    {
        instance=[[super allocWithZone:NULL] init];
    });
    
    return instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone
{
    return [self getInstance];
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (NSMutableDictionary *)mdicALert
{
    if (!_mdicALert)
    {
        _mdicALert = [NSMutableDictionary dictionaryWithCapacity:0];
    }
    
    return _mdicALert;
}

/**
 统一处理session过期情况

 @param task 发送请求的task
 @param back session过期的回调
 */
- (void)handelTask:(MPURLSessionTask *)task sessionExBlock:(void(^)(MPE_NetWorkSessionState))back
{
    /*
     TODO: 处理section过期流程
     */
}

/**
 只展示同类型的一个aletView（调用多次也只是创建一个）
 
 @param alertId alertView的唯一ID
 @param text 提示信息
 @param title 提示标题
 */
- (void)showSingleAlertViewById:(NSString *)alertId message:(NSString *)text withTitle:(NSString *)title
{
    if ([self.mdicALert objectForKey:alertId])
    {
        return;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:text
                                                   delegate:self
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [alert show];
    
    [self.mdicALert setValue:alert forKey:alertId];
    
    
    
}

/**
 alertViewDelegate

 @param alertView alertView
 @param buttonIndex 点击的按钮index
 */
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [self.mdicALert removeObjectForKey:[[self.mdicALert allKeysForObject:alertView] firstObject]];
}


@end
